package org.koyabr.webx.demo.danmaku.web.ajax.module.screen;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.citrus.turbine.Context;
import org.koyabr.webx.demo.danmaku.biz.UserManager;
import org.koyabr.webx.demo.danmaku.dal.dataobjs.User;
import org.koyabr.webx.demo.danmaku.web.common.DanmakuUser;
import org.springframework.beans.factory.annotation.Autowired;

public class Follow {
	@Autowired
	private UserManager userManager;

	@Autowired
	private HttpServletRequest request;

	public void execute(Context context) throws Exception {
		String targetUsername = request.getParameter("username");
		User targetUser;
		if (targetUsername == null
				|| (targetUser = userManager.getUser(targetUsername)) == null)
			return;

		User currentUser = userManager.getUser(DanmakuUser.getCurrentUser()
				.getUsername());
		if(currentUser == null)
			return;
		
		if(currentUser.followSet.contains(targetUser)){
			currentUser.followSet.remove(targetUser);
		}
		else if(!currentUser.getUsername().equals(targetUser.getUsername())){
			currentUser.followSet.add(targetUser);
		}
		
		context.put("followNum", targetUser.fanSet.size());
	}
}
